package com.slofzx.nuoda.evaluation.param.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.slofzx.nuoda.common.util.IdGen;
import com.slofzx.nuoda.evaluation.param.dto.CommonUnitCostDTO;
import com.slofzx.nuoda.evaluation.param.entity.CommonUnitCost;
import com.slofzx.nuoda.evaluation.param.entity.CommonYearRelated;
import com.slofzx.nuoda.evaluation.param.dto.CommonYearRelatedDTO;
import com.slofzx.nuoda.evaluation.param.vo.CommonYearRelatedVO;
import com.slofzx.nuoda.evaluation.param.dao.CommonYearRelatedDAO;
import com.slofzx.nuoda.evaluation.param.service.CommonYearRelatedService;
import com.slofzx.nuoda.evaluation.project.util.BusiUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 公共参数年度相关服务实现类
 *
 * @author nuoda
 * @since 2023-10-17 16:03:40
 */
@Service("commonYearRelatedService")
public class CommonYearRelatedServiceImpl extends BaseServiceImpl implements CommonYearRelatedService {
    @Resource
    private CommonYearRelatedDAO commonYearRelatedDAO;

    /**
     * 将数据表数据转换为返回数据类型
     *
     * @param entity 数据表对应类
     * @return 实例对象
     */
    protected CommonYearRelatedVO parseEntity2VO(CommonYearRelated entity) {
        CommonYearRelatedVO vo = new CommonYearRelatedVO();
        vo.setId(entity.getId());
        vo.setVersionId(entity.getVersionId());
        vo.setYear(entity.getYear());
        vo.setOperateCostAdjustFactor(entity.getOperateCostAdjustFactor());
        vo.setCreateUser(entity.getCreateUser());
        vo.setCreateTime(entity.getCreateTime());
        return vo;
    }

    /**
     * 将数据传递类转换为数据表数据类
     *
     * @param dto 数据传递类实例
     * @return 实例对象
     */
    protected CommonYearRelated parseDTO2Entity(CommonYearRelatedDTO dto) {
        CommonYearRelated entity = new CommonYearRelated();
        entity.setId(dto.getId());
        entity.setVersionId(dto.getVersionId());
        entity.setYear(dto.getYear());
        entity.setOperateCostAdjustFactor(dto.getOperateCostAdjustFactor());
        return entity;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CommonYearRelatedVO queryById(String id) {
        CommonYearRelated entity = this.commonYearRelatedDAO.selectById(id);
        if (entity == null) {
            return null;
        }
        return parseEntity2VO(entity);
    }

    /**
     * 查询多条数据
     *
     * @param dto 数据传递类实例对象
     * @return 对象列表
     */
    @Override
    public List<CommonYearRelatedVO> queryList(CommonYearRelatedDTO dto) {
        //mybatis-plus 写法示例
        //IPage<CommonYearRelated> pageResult = this.evaModelDAO.selectPage(new Page<>(dto.getPageNo(),dto.getPageSize()),null);
        //List<CommonYearRelated> dataList = pageResult.getRecords();
        //dto.setCount((int) pageResult.getTotal());
        List<CommonYearRelated> dataList = this.commonYearRelatedDAO.queryList(dto);
        if (null == dataList || dataList.isEmpty()) {
            return Collections.emptyList();
        }
        List<CommonYearRelatedVO> resultList = new ArrayList(dataList.size());
        for (CommonYearRelated item : dataList) {
            CommonYearRelatedVO vo = parseEntity2VO(item);
            resultList.add(vo);
        }
        // TODO 添加其他关联查询数据
        return resultList;
    }

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult create(CommonYearRelatedDTO dto) {
        // TODO 添加校验逻辑
        CommonYearRelated entity = parseDTO2Entity(dto);
        preInsert(entity);
        this.commonYearRelatedDAO.insert(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult update(CommonYearRelatedDTO dto) {
        String id = dto.getId();
        if (StringUtils.isEmpty(id)) {
            return CommonResultEnum.NOT_EXIST;
        }
        CommonYearRelated entity = this.commonYearRelatedDAO.selectById(id);
        if (entity == null) {
            return CommonResultEnum.NOT_EXIST;
        }
        // TODO 添加其他校验逻辑，如数据权限

        entity.setVersionId(dto.getVersionId());
        entity.setYear(dto.getYear());
        entity.setOperateCostAdjustFactor(dto.getOperateCostAdjustFactor());
        this.commonYearRelatedDAO.updateById(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 操作结果
     */
    @Override
    public ServiceResult deleteById(String id) {
        // TODO 添加校验逻辑，如数据权限
        this.commonYearRelatedDAO.deleteById(id);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    @Transactional
    public ServiceResult save(String versionId, List<CommonYearRelatedDTO> dtoList) {
        if (dtoList == null || dtoList.isEmpty()) {
            return CommonResultEnum.SUCCESS;
        }
        this.deleteByVersion(versionId);
        List<CommonYearRelated> insertList = new ArrayList<>();
        for (CommonYearRelatedDTO dto : dtoList) {
            CommonYearRelated entity = parseDTO2Entity(dto);
            preInsert(entity);
            entity.setVersionId(versionId);
            insertList.add(entity);
        }
        commonYearRelatedDAO.insertBatch(insertList);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public ServiceResult deleteByVersion(String versionId) {
        QueryWrapper<CommonYearRelated> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CommonYearRelated::getVersionId, versionId);
        commonYearRelatedDAO.delete(queryWrapper);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public List<CommonYearRelatedVO> queryListByVersion(String versionId) {
        CommonYearRelatedDTO commonYearRelatedDTO = new CommonYearRelatedDTO();
        commonYearRelatedDTO.setVersionId(versionId);
        return queryList(commonYearRelatedDTO);
    }

    @Override
    public void copy(String fromId, String toId) {
        final CommonYearRelatedDTO dto = new CommonYearRelatedDTO();
        dto.setVersionId(fromId);
        final List<CommonYearRelated> commonYearRelateds = commonYearRelatedDAO.queryList(dto);
        if (CollectionUtils.isEmpty(commonYearRelateds)) {
            return;
        }

        final List<CommonYearRelated> collect = commonYearRelateds.stream().map(m -> {
            final CommonYearRelated deepCopy = BusiUtil.deepCopy(m, CommonYearRelated.class);
            deepCopy.setId(IdGen.uuid());
            deepCopy.setCreateTime(new Date());
            deepCopy.setVersionId(toId);
            return deepCopy;
        }).collect(Collectors.toList());

        commonYearRelatedDAO.insertBatch(collect);
    }

}
